package com.sgb.knowledgeshare;

import android.app.Activity;
import android.content.Context;

import com.alibaba.android.arouter.facade.template.IProvider;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.knowledgeshare.service.ShareModuleApi;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.AppFolwLogApi;
import com.swgk.core.base.aroute.provider.ShareProvider;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.Map;

public class ShareConfig {
    public static  final  String RESPONSE_OK = "ok";
    public static final String BASE_URL_RELEASE = "http://gateway.shigongbang.com/sgb-app-android/";
    public static final String BASE_URL_PRE = "https://api.jingyingbang.net/sgb-app/";
    public static final String BASE_URL_DEBUG = "http://api.sgbwl.com/sgb-app/";
    private static ShareConfig instance;
    private static String baseUrl ="";
    private ShareModuleApi api;
    private APIManager apiManger;
    private AppFolwLogApi appFolwLogApi;
    private final Map<Class<? extends IProvider>, IProvider> iProviders;

    public static ShareConfig getInstance() {
        if (instance == null) {
            synchronized (ShareConfig.class) {
                if (instance == null) {
                    instance = new ShareConfig();
                }
            }
        }
        return instance;
    }

    public static void init(Context context, String buildType) {
        SharedPreferenceUtil.init(context);
        if ("release".equals(buildType)) {
            baseUrl = ShareConfig.BASE_URL_RELEASE;
            MLog.isOnlin = true;
        } else if ("pre".equals(buildType)) {
            baseUrl = ShareConfig.BASE_URL_PRE;
            MLog.isOnlin = false;
        } else if ("debug".equals(buildType)) {
            baseUrl = ShareConfig.BASE_URL_DEBUG;
            MLog.isOnlin = false;
        }
        getInstance().changeApi();
    }

    private ShareConfig() {
        iProviders = new HashMap<>();
    }
    public ShareModuleApi getApi() {
        if (api == null) {
            api = changeApi();
        }
        return api;
    }

    public ShareModuleApi changeApi() {
        if (apiManger == null){
            apiManger = APIManager.getInstance(baseUrl);
        }
        api = apiManger.create(ShareModuleApi.class);
        return api;
    }

    public AppFolwLogApi AppFolwLogApi() {
        if (apiManger == null){
            apiManger = APIManager.getInstance(baseUrl);
        }
        appFolwLogApi = apiManger.create(AppFolwLogApi.class);
        return appFolwLogApi;
    }

    /**
     * 知识分享分享实现
     * @param context
     * @param title
     * @param url
     * @param cover
     * @param i
     */
    public void startShareData(Activity context, String title, String url, String cover, int i) {
        ShareProvider navigation = getProvider(ShareProvider.class);
        if (navigation != null) {
            navigation.startShareData(context,title, url, cover,i);
        }
    }

    public <T extends IProvider> T getProvider(Class<T> service) {
        T navigation = (T) iProviders.get(service);
        if (navigation == null) {
            navigation = ARouter.getInstance().navigation(service);
            iProviders.put(service, navigation);
        }
        return navigation;
    }
}
